package com.summer.boss.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

@EnableSwagger2WebMvc
@EnableKnife4j
@Configuration
public class SwaggerConfiguration {

	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(this.apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.summer.boss"))
				.paths(PathSelectors.any())
				.build().globalOperationParameters(buildGlobalParameters());
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("SUMMER-BOSS接口文档")
				.description("summer boss backend api")
				.version("1.0")
				.build();
	}

	private List<Parameter> buildGlobalParameters() {
		List<Parameter> globalParams = new ArrayList<>();
		ParameterBuilder tokenPar = new ParameterBuilder();
		Parameter token = tokenPar.name("token").description("请求token")
				.modelRef(new ModelRef("string"))
				.parameterType("header")
				.required(false).order(1)
				.build();
		Parameter requestId = tokenPar.name("requestId").description("请求唯一标识")
				.modelRef(new ModelRef("string"))
				.parameterType("header")
				.required(false).order(2)
				.build();
		Parameter currentUserId = tokenPar.name("currentUserId").description("用户id")
				.modelRef(new ModelRef("string"))
				.parameterType("header")
				.required(false).order(3)
				.build();
		Parameter currentUserName = tokenPar.name("currentUserName").description("用户名称")
				.modelRef(new ModelRef("string"))
				.parameterType("header")
				.required(false).order(4)
				.build();
		globalParams.add(token);
		globalParams.add(requestId);
		globalParams.add(currentUserId);
		globalParams.add(currentUserName);
		return globalParams;
	}
}

